package com.example.hanfu.zhuanti;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.example.hanfu.R;
import com.example.hanfu.WebViewActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ZhuanTiFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ZhuanTiFragment extends Fragment implements AdapterView.OnItemClickListener{
    ListView listView;
    ZhuanTiAdapter adapter;
    private static final String TAG = "ZhuanTiFragment";
    Button yiyi,sheying,zixun,zhishi,huodong,shici;


    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public ZhuanTiFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ZhuanTiFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ZhuanTiFragment newInstance(String param1, String param2) {
        ZhuanTiFragment fragment = new ZhuanTiFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_zhuan_ti, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = view.findViewById(R.id.zhuanti_list);
        /*
        sheying=view.findViewById(R.id.sheying);
        zixun=view.findViewById(R.id.zixun);
        zhishi=view.findViewById(R.id.zhishi);
        shici=view.findViewById(R.id.shici);
        yiyi=view.findViewById(R.id.yiyi);
        huodong=view.findViewById(R.id.huodong);

         */
        listView.setOnItemClickListener(this);
        GridView gridview=view.findViewById(R.id.zhuanti);

        List<String> list1=new ArrayList<String>();
       list1.add("汉服摄影");
       list1.add("汉服咨询");
       list1.add("汉服科普");
       list1.add("汉服活动");
       list1.add("诗词歌赋");
       list1.add("汉服意义");

        ListAdapter adaper=new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,list1);
        gridview.setAdapter(adaper);

        //listView.setOnItemLongClickListener(this);
        hander("https://hanfusong.com/%e6%b1%89%e6%9c%8d%e7%9f%a5%e8%af%86");
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
                if(position==0){
                    hander("https://hanfusong.com/archives/tag/%e6%b1%89%e6%9c%8d%e6%b4%bb%e5%8a%a8");
                }else if(position==1){
                    hander("https://hanfusong.com/uncategorized");
                }else if(position==2){
                    hander("https://hanfusong.com/%e6%b1%89%e6%9c%8d%e7%9f%a5%e8%af%86");
                }else if(position==3){
                    hander("https://hanfusong.com/archives/tag/%e6%b1%89%e6%9c%8d%e6%b4%bb%e5%8a%a8");
                }else if(position==4){
                    hander("https://hanfusong.com/archives/tag/%e6%96%87%e5%ad%a6%e8%af%97%e8%af%8d");
                }else if(position==5){
                    hander("https://hanfusong.com/archives/tag/%e6%b1%89%e6%9c%8d%e6%84%8f%e4%b9%89");
                }




            }

        });
        /*
        sheying.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hander("https://hanfusong.com/archives/tag/%e6%b1%89%e6%9c%8d%e6%b4%bb%e5%8a%a8");
            }
        });
        zixun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hander("https://hanfusong.com/uncategorized");
            }
        });
        zhishi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hander("https://hanfusong.com/%e6%b1%89%e6%9c%8d%e7%9f%a5%e8%af%86");
            }
        });
        huodong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                hander("https://hanfusong.com/archives/tag/%e6%b1%89%e6%9c%8d%e6%b4%bb%e5%8a%a8");
            }
        });
        shici.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hander("https://hanfusong.com/archives/tag/%e6%96%87%e5%ad%a6%e8%af%97%e8%af%8d");
            }
        });
        yiyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hander("https://hanfusong.com/archives/tag/%e6%b1%89%e6%9c%8d%e6%84%8f%e4%b9%89");
            }
        });

         */
    }

    private void hander(String url) {

        Handler handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 1) {

                    adapter = new ZhuanTiAdapter(getContext(), R.layout.zhuanti_item, (ArrayList
                            <ZhuanTi>) msg.obj);
                    listView.setAdapter(adapter);

                }
                super.handleMessage(msg);
            }
        };
        ZhuanTiTask task = new ZhuanTiTask(url);
        task.setHandler(handler);
        Thread t = new Thread(task);
        t.start();

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ZhuanTi item =(ZhuanTi) listView.getItemAtPosition(position);
        String title=item.getTitle();
        String href=item.getHref();
       // Bitmap img=item.getImg();
        Log.i(TAG, "onItemClick:title" + title);
        Log.i(TAG, "onItemClick:href:" + href);
        //Log.i(TAG, "onItemClick:img:" + img);
        Intent openHref=new Intent(getContext(), WebViewActivity.class)
                .putExtra("url",href);
        Log.i(TAG, "openWebViewActivity:url=" + href);
        startActivity(openHref);

        // adapter.remove(map);

        //show(countryStr,rateStr);

    }
/*
    public void click(View v){
        if(v.getId()==R.id.sheying){
            hander("https://hanfusong.com/hfsy");

        }else if(v.getId()==R.id.zixun){
            hander("https://hanfusong.com/uncategorized");

        }else if(v.getId()==R.id.zhishi){
            hander("https://hanfusong.com/%e6%b1%89%e6%9c%8d%e7%9f%a5%e8%af%86");

        }else if(v.getId()==R.id.huodong){
            hander("https://hanfusong.com/archives/tag/%e6%b1%89%e6%9c%8d%e6%b4%bb%e5%8a%a8");

        }else if(v.getId()==R.id.shici){
            hander("https://hanfusong.com/archives/tag/%e6%96%87%e5%ad%a6%e8%af%97%e8%af%8d");

        }else if(v.getId()==R.id.lishi){
            hander("https://hanfusong.com/lishi");

        }
}

 */

}